package com.fingard.dsp.bank.reqrespobj.balance;

import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.dsp.bank.reqrespobj.balance.detail.Req7506Detail;
import com.fingard.xml.XmlReadMode;
import com.fingard.xml.XmlTextReader;

public class Req7506BalQry extends RequestBase {

    public boolean isBatch = false;//是否为批量余额查询
    public int detailCount = -1;
    public String savingType="";

    private XmlTextReader xmlReader;

    public Req7506BalQry(XmlTextReader p_xmlReader, ReqRespHeader p_header) throws Exception {
        reqHeader = p_header;
        xmlReader = p_xmlReader;
        detailCount = xmlReader.countElement(PATH_Detail);
        //savingType = xmlReader.getSimpleText("SavingType");
        if (detailCount > 0) {
            isBatch = true;
        } else {
            isBatch = false;
            detailCount = 1;
        }
    }

    public void backToTop(){
        xmlReader.backToTop();
        readCount = 0;
    }

    private int readCount = 0;
    public Req7506Detail readedDetail;

    public boolean readRecord() throws Exception {
        readedDetail = null;
        int tmpCount = readCount++;
        if (isBatch) {
            if (xmlReader.read(PATH_Detail)) {//  /ATSYH/TransReq/TransParam/DetailRecord
                readedDetail = new Req7506Detail(xmlReader.readedXElement, reqHeader.bankCode);
                return true;
            } else {
                return false;
            }
        } else {
            if (tmpCount > 0) {//已经读过了
                return false;
            } else {
                if (xmlReader.read("/ATSYH/TransReq/TransParam")) {
                    readedDetail = new Req7506Detail(xmlReader.readedXElement, reqHeader.bankCode);
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    public void resetReadMode(){
        readCount = 0;
        xmlReader.resetReadMode(XmlReadMode.None);
    }

	/*
	public void fromXmlReader(Element p_element){
		listReq7506 = new ArrayList<Req7506Detail>();
		NodeList list = p_element.getElementsByTagName("DetailRecord");
	    if(list != null && list.getLength() > 0){
	    	isBatch = true;
	    	for(int i=0;i<list.getLength();i++){
	    		Req7506Detail tmpDetail = new Req7506Detail();
	    		tmpDetail.fromElement((Element)list.item(i));
	    		listReq7506.add(tmpDetail);
	    	}
	    }
	    else{
	    	Req7506Detail tmpDetail = new Req7506Detail();
    		tmpDetail.fromElement(p_element);
    		listReq7506.add(tmpDetail);
	    }
	}
	*/

    public boolean isSameActBankType() throws Exception {
        boolean retValue = true;
        xmlReader.resetReadMode(XmlReadMode.XElementReader);
        boolean hasCommonAct = false;
        boolean hasSpecialAct = false;
        while (xmlReader.read(PATH_Detail + "/HigherLevelAct")) {
            if (xmlReader.readedXElement.innerText.length() == 0) {
                hasCommonAct = true;
            } else {
                hasSpecialAct = true;
            }
            if (hasCommonAct && hasSpecialAct) {
                retValue = false;
                break;
            }
        }
        xmlReader.resetReadMode(XmlReadMode.XElementReader);
        return retValue;
    }

    public String getOriReqXml(){
        return xmlReader.getOriXmlString();
    }
}
